package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.dto.warehouse.UpdateWareDTO;
import io.github.talelin.latticy.dto.warehouse.WarehouseDTO;
import io.github.talelin.latticy.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.WarehouseDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import java.util.List;


/**
 * 仓库前端控制器
 *
 * @author generator@wulongbo
 * @since 2022-05-10
 */
@RestController
@RequestMapping("/v1/warehouse")
@Validated
@PermissionModule(value = "仓库")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @PostMapping("")
    @AdminRequired
    @PermissionMeta(value = "创建仓库", mount = false)
    @Logger(template = "{user.username}创建仓库:{request.name}")
    public CreatedVO create(@RequestBody @Validated WarehouseDTO validator) {
        warehouseService.createWarehouse(validator);
        return new CreatedVO();
    }

    @AdminRequired
    @PermissionMeta(value = "更新仓库", mount = false)
    @PutMapping("/{code}")
    public UpdatedVO update(@PathVariable String code
            , @RequestBody @Validated UpdateWareDTO validator) {
        warehouseService.updateWarehouse(code, validator);
        return new UpdatedVO();
    }

    @LoginRequired
    @GetMapping("/{code}")
    public WarehouseDO get(@PathVariable(value = "code") String code) {
        return warehouseService.getWarehouseByCode(code);
    }

    @GetMapping("/page")
    @PermissionMeta(value = "查询所有仓库", mount = false)
    @AdminRequired
    public PageResponseVO<WarehouseDO> page(@Validated BasePageDTO dto) {
        IPage<WarehouseDO> iPage = warehouseService.getWarePage(dto.getPage(), dto.getCount());
        return PageUtil.build(iPage);
    }

    @LoginRequired
    @GetMapping("/list")
    public List<WarehouseDO> list() {
        return warehouseService.getWareList();
    }

}
